home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / rend10.lzh / REND1.0 / GraphicSubSystem / viewperstrans.c < prev   
Encoding:
C/C++ Source or Header  |  1994-02-25  |  6.1 KB  |  229 lines

  1. /*
  2.  * Title:
  3.  *    viewperstrans.c
  4.  *
  5.  * Authors:
  6.  *    Michael P. Schenck
  7.  *
  8.  * Purpose:
  9.  *    This module provides the view structure used to describe the 
  10.  *    position of the viepoint and the structure of the view
  11.  *    volume.  This view volume is used for clipping everything 
  12.  *    outside of it out of the raster list.  A view structure can
  13.  *     be allocated and then configured to be used as the current 
  14.  *    view.  After this, if displaygraphics() is called, it will
  15.  *    use this new view point.  The view is controlled by specifying
  16.  *     a point you are looking at and then spherical coordinates to
  17.  *    the point you are looking from.  D and f are the parameters
  18.  *    that control the far and near clipping plane. 
  19.  *    The clipping implimented clips entire polygons if any of thier
  20.  *    verticies are outside the view volume.  This can create some
  21.  *     bizzare visual artifacts if you have large polygons and are up
  22.  *    close to them.  They could randomly appear and dissapear.
  23.  *    If a view structure could not be allocated, a NULL pointer is
  24.  *    returned.      
  25.  *
  26.  * Copyright Info:
  27.  *    Copyright (C) 1993, 1994 -- by Michael P. Schenck, 
  28.  *    (mps4466@ultb.isc.rit.edu)
  29.  *    
  30.  *    This program is free software; you can redistribute it and/or modify
  31.  *    it under the terms of the GNU General Public License as published
  32.  *    by the Free Software Foundation; either version 2 of the License,
  33.  *    or (at your option) any later version.
  34.  *
  35.  *    This software is distributed in the hope that it will be useful, but
  36.  *    WITHOUT ANY WARRANTY; without even the implied warranty of
  37.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38.  *    GNU General Public License for more details.
  39.  *
  40.  *      For a copy of the GNU General Public License
  41.  *    write to the Free Software Foundation, 675 Mass Ave,
  42.  *    Cambridge, MA  02139, USA.
  43.  *
  44.  */
  45.  
  46. #include <stdlib.h>
  47. #include <math.h>
  48. #include "/include/types.h"
  49. #include "/include/errors.h"
  50. #include "/include/matrix.h"
  51. #include "/include/database.h"
  52. #include "/include/viewperstrans.h"
  53.  
  54. ULONG objectviewlist[MAXPOLYINVIEW],
  55.       polyviewlist[MAXPOLYINVIEW],
  56.       viewlistindex;
  57.  
  58. extern struct Object *objects[MAXNUMOBJECTS];
  59. extern UWORD height,x_center,y_center;         
  60.        
  61. static FLOAT sintheta,costheta,sinphi,cosphi,scale,*v,*p,*rm;
  62.  
  63.     /* Open and allocate required matricies. */
  64.  
  65. UBYTE openviewperstrans()
  66.  
  67. {
  68.    if((v = allocatematrix())==NULL)
  69.        return(MEM_ALLOC_FAILURE);
  70.    if((p = allocatematrix())==NULL) {
  71.     freematrix(v);
  72.     return(MEM_ALLOC_FAILURE);
  73.    }
  74.    if((rm = allocatematrix())==NULL) {
  75.     freematrix(v);
  76.     freematrix(p);
  77.     return(MEM_ALLOC_FAILURE);
  78.    }
  79.    return(SUCCESS);
  80. }
  81.  
  82.     /* Get a raw view structure. */
  83.  
  84. struct View *allocateview()
  85.  
  86. {
  87.    return((struct View *)malloc(sizeof(struct View)));
  88. }
  89.  
  90.     /* Configure the view by calculating new viewing and perspective 
  91.        transformation. */
  92.  
  93. void configureview(struct View *view)
  94.  
  95. {       
  96.    scale = (float)height;    /* change to a constant smaller than the height and you will
  97.                       see the clipping occur for the entire view volume */
  98.    sintheta = sin(view->theta);
  99.    costheta = cos(view->theta); 
  100.    sinphi = sin(view->phi);
  101.    cosphi = cos(view->phi);
  102.   
  103.       /* Construct viewing matrix. */
  104.    
  105.    *(v) = -sintheta;
  106.    *(v+1) = -cosphi * costheta;
  107.    *(v+2) = -sinphi * costheta;
  108.    *(v+3) = 0.0;
  109.    *(v+4) = costheta;
  110.    *(v+5) = -cosphi * sintheta;
  111.    *(v+6) = -sinphi * sintheta;
  112.    *(v+7) = 0.0;
  113.    *(v+8) = 0.0;
  114.    *(v+9) = sinphi;
  115.    *(v+10) = -cosphi;
  116.    *(v+11) = 0.0;
  117.    *(v+12) = view->x*sintheta-view->y*costheta;
  118.    *(v+13) = view->x*cosphi*costheta+view->y*cosphi*sintheta-view->z*sinphi;
  119.    *(v+14) = view->ro+view->x*sinphi*costheta+view->y*sinphi*sintheta+view->z*cosphi;
  120.    *(v+15) = 1.0;
  121.  
  122.     /* Construct and concatenate perspective transformation matrix. */
  123.  
  124.    identitymatrix(p);
  125.  
  126.    *(p) = scale;
  127.    *(p+5) = scale;
  128.    *(p+10) = view->f/(view->f-view->d);
  129.    *(p+11) = 1.0;
  130.    *(p+14) = -(view->d*view->f/(view->f-view->d));
  131.    *(p+15) = 0.0;
  132.  
  133.    multmatrix(v,p,rm);
  134. }
  135.  
  136.     /* Free up a view structure. */
  137.  
  138. void releaseview(struct View *view) 
  139.  
  140. {
  141.    free((void *)view);
  142. }
  143.  
  144.     /* Transform all the verticies in the view list, clip the polys,
  145.        and build a raster list. */ 
  146.  
  147. void transformvert()
  148. {    
  149.    register ULONG i,j,k,l;
  150.    UBYTE *vertflag;
  151.    ULONG *polygons,inside,curvert;    
  152.    FLOAT *verticies,*transvert,w,*rv;
  153.    
  154.    viewlistindex = 0;
  155.    for(i=0;i<MAXNUMOBJECTS;i++) {
  156.       if(objects[i] != NULL) {
  157.     
  158.      vertflag = objects[i]->vertflag;
  159.      polygons = objects[i]->polygons;
  160.      transvert = objects[i]->transvert;
  161.      verticies = objects[i]->verticies;
  162.     
  163.      for(j=0;j<(objects[i]->numvert);j++)
  164.               *(vertflag+j) = 0;
  165.           
  166.      for(j=0;j<objects[i]->numpoly*MAXPOLYVERT;j+=MAXPOLYVERT) {
  167.         inside = TRUE;
  168.         for(k=0;k<MAXPOLYVERT;k++) {
  169.            if((curvert = *(polygons+j+k))==NOVERT)
  170.               k=MAXPOLYVERT;
  171.            else {
  172.               if(!(*(vertflag+curvert))) {
  173.                  rv = transvert+curvert*4;
  174.              
  175.              /* Viewing transformation. */
  176.              
  177.                  multvecandmat(verticies+curvert*4,rm,rv);
  178.  
  179.                   /* Add clipping buffer to reduce visual artifacs inherent
  180.                 in this method of clipping (removing entire polygons!). */
  181.  
  182.              w = scale * (*(rv+3) + *(rv+3)*0.2); 
  183.              
  184.              /* 3D clipping in homogeneous coords. */ 
  185.               
  186.              if((*rv>w)||(*rv<-w)||(*(rv+1)>w)||(*(rv+1)<-w)||(*(rv+2)<0.0)||(*(rv+2)>*(rv+3))) {
  187.                  *(vertflag+curvert) += 1;
  188.             inside = FALSE;
  189.             k = MAXPOLYVERT;
  190.              }
  191.              else {
  192.                           for(l=0;l<3;l++)
  193.                        *(rv+l) = (*(rv+l)) / (*(rv+3));
  194.                         *rv = *rv + x_center;
  195.                     *(rv+1) = *(rv+1) + y_center;
  196.              }
  197.                  *(vertflag+curvert) += 1; 
  198.               }
  199.           else {
  200.              if(*(vertflag+curvert)==2){
  201.               inside = FALSE;
  202.             k = MAXPOLYVERT;
  203.              }
  204.           }
  205.            } 
  206.             }
  207.         if(inside) {
  208.            objectviewlist[viewlistindex] = i;
  209.            polyviewlist[viewlistindex] = j / MAXPOLYVERT;
  210.             viewlistindex++;
  211.            if(viewlistindex == MAXPOLYINVIEW)
  212.               return;
  213.         }
  214.          }
  215.       }
  216.    }   
  217. }   
  218.  
  219.     /* Close module. */
  220.  
  221. void closeviewperstrans()
  222.  
  223. {
  224.    freematrix(v);
  225.    freematrix(p);
  226.    freematrix(rm);
  227.    
  228. }
  229.